﻿using System;
using System.Collections.Generic;
using System.Text;

namespace JustMathCore
{
    public class CMD
    {
        Dictionary<string, Action<string[]>> myactions = new Dictionary<string, Action<string[]>>();
        public void RegAction(string key, Action<string[]> action)
        {
            if (key == "help")
                throw new Exception("not support help anymore.");
            myactions[key.ToLower()] = action;
        }
        public void RegHelpAction(Action helpaction)
        {
            Action<string[]> action = (argv) =>
            {
                helpaction();
            };
            myactions["help"] = action;
        }
        public void Main(string[] argv)
        {
            if (argv.Length == 0)
            {
                Console.WriteLine("Just Math All in One.");
                Console.WriteLine("v0.01");

                Write(CmdRule.WriteType.Result, "NeedParam");
        
                return;
            }
            else
            {

                var key = argv[0].ToLower();
                if (myactions.TryGetValue(key, out Action<string[]> doact))
                {
                    doact(argv);
                }
                else
                {
                    Write(CmdRule.WriteType.Result, "NeedParam");
                }
            }
        }
        public readonly Action<CmdRule.WriteType, string> Write = CmdRule.Write;
        public readonly Func<string, Int32> ParseI32 = CmdRule.ParseI32;
        public readonly Func<string, byte> ParseU8 = CmdRule.ParseU8;
        public readonly Func<string, UInt16> ParseU16 = CmdRule.ParseU16;
        public readonly Func<string, UInt32> ParseU32 = CmdRule.ParseU32;

    }
}
